package digitaldreamers.libraries.networkmanagement.socket;

import java.io.IOException;
import java.util.List;

import digitaldreamers.libraries.networkmanagement.socket.ConnectionHandler.IOnDataReceiverListener;
import android.app.ProgressDialog;
import android.os.SystemClock;

public abstract class NetworkScreen implements IOnDataReceiverListener, Runnable {
	public List<byte[]> dataList;
	public Thread packetHandleThread;
	public volatile boolean bRunning = false;
	public ConnectionHandler connectionHandler;
	public ProgressDialog mDialog;
	public NetworkScreen() {
		packetHandleThread = new Thread(this);
	}

	@Override
	public abstract void onDataReceiver(byte[] data) throws IOException;
	
	public synchronized void addPacket(byte[] data) {
        dataList.add(data);
    }
    
    public synchronized void removePacket(int iIndex) {
        dataList.remove(iIndex);
    }
    
    public abstract void handlePacket(byte[] data) throws Exception;

    public void run() {
		// TODO Auto-generated method stub
		while (bRunning) {
			if (dataList.size() > 0) {
				byte[] data = dataList.get(0);
				removePacket(0);
				try {
					handlePacket(data);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
			SystemClock.sleep(100);
		}
	}
}
